<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'scientificInstitutions:add'"
          >新增</el-button
        >
        <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'scientificInstitutions:export'"
          >下载数据</el-button
        >
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入研究中心名称"
          v-model="parameter.name"
          @keydown.enter.native="search"
          clearable
          @clear="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
        <!-- <el-select
          placeholder="类型"
          v-model="parameter.type"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in scienceTypeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select> -->
        <!-- <el-select
          placeholder="领域方向"
          v-model="parameter.directWay"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in scienceFieldOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select> -->
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="机构名称"
        prop="name"
        show-overflow-tooltip
        width="300"
      />
      <!-- <el-table-column
        label="类型"
        prop="type"
        show-overflow-tooltip
      /> -->
      <!-- <el-table-column
        label="领域方向"
        prop="directWay"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="科研项目数量"
        prop="projectCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="申报专利数量"
        prop="patentCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="可转化成果数量"
        prop="transformCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="孵化企业数"
        prop="hatchCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="科研平台数"
        prop="platformCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="首席科学家"
        prop="chiefScientist"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="联系人及联系方式"
        prop="contract"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'scientificInstitutions:edit'"
            >编辑</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'scientificInstitutions:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  scientificInstitutionsQuery,
  deleteScientificInstitutions,
  downloadScientificInstitutions
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import {
  scienceTypeOptions,
  scienceFieldOptions
} from '@/utils/project/data'
import { downloadBlobFile } from '@/utils/download'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'scientificInstitutionsList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        name: '',
        // directWay: '',
        // type: '',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      governProjectOptions: [
        {
          dictValue: '政府投资',
          dictText: '政府投资'
        },
        {
          dictValue: '社会投资',
          dictText: '社会投资'
        }
      ],
      loading: false,
      showDialog: false
    }
  },
  computed: {
    scienceTypeOptions() {
      return scienceTypeOptions
    },
    scienceFieldOptions() {
      return scienceFieldOptions
    }
  },
  mounted() {
    this.getList()
  },
  activated() {
    if (this.$route.query.refresh) {
      this.getList()
      this.$router.replace({
        path: this.$route.path,
        query: { ...this.$route.query, refresh: undefined }
      })
    }
  },
  methods: {
    search() {
      this.parameter.name =
        this.parameter.name?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      this.loading = true
      scientificInstitutionsQuery(this.parameter).then(
        (res) => {
          if (res.success) {
            this.total = res.data.count
            this.projectList = res.data.list
          }
          this.loading = false
        }
      )
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      this.$router.push({
        path: '/dataManagement/technologyManagement/scientificInstitutions/add',
        query: {
          title: '新增'
        }
      })
    },

    // 编辑
    editProject(row) {
      this.$router.push({
        path: '/dataManagement/technologyManagement/scientificInstitutions/add',
        query: {
          pkid: row.pkid,
          data: row,
          title: '编辑',
          isEdit: true
        }
      })
    },
    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/dataManagement/technologyManagement/scientificInstitutions/add',
        query: {
          pkid: row.pkid,
          data: row,
          title: '查看',
          isEdit: true,
          isDetail: true
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该项目，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteScientificInstitutions(row.pkid).then(
            (res) => {
              if (res.success) {
                this.$message.success('删除成功')
                if (
                  this.total % 10 === 1 &&
                  this.parameter.pageIndex ===
                    parseInt(this.total / 10) + 1 &&
                  this.parameter.pageIndex !== 1
                ) {
                  this.parameter.pageIndex -= 1
                }
                this.getList()
              } else {
                this.$message.error(res.message)
              }
            }
          )
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      this.btnLoading = true
      downloadScientificInstitutions(this.parameter)
        .then((res) => {
          downloadBlobFile(res, '科研机构信息表.xlsx')
        })
        .catch((err) => {
          this.$message.error('下载失败')
        })
        .finally(() => {
          this.btnLoading = false
        })
    },
    handleSubmit(data) {
      console.log('提交进度数据:', data)
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
